package com.openvid.webrtc;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.openvid.webrtc.databinding.ActivityMain2Binding;
import com.openvid.webrtc.openvid.RtcRoomDelegate;
import com.openvid.webrtc.openvid.Session;
import com.openvid.webrtc.openvid.http.RtcRoomerClient;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

public class MainActivity extends AppCompatActivity {

    private ActivityMain2Binding mBinding;

    RtcRoomerClient mClient = new RtcRoomerClient("https://test-live-xy.deepblueai.com/openvidu/"
            , "OPENVIDUAPP"
            , "password");


    RtcRoomDelegate mRtcRoomDelegate1;
    RtcRoomDelegate mRtcRoomDelegate2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.inflate(
                LayoutInflater.from(this),
                R.layout.activity_main2,
                null,
                false
        );
        setContentView(mBinding.getRoot());


        mBinding.start1.setOnClickListener(v -> getRoomRoken("room1"));

        mBinding.stop1.setOnClickListener(v -> {
            mRtcRoomDelegate1.stop();
        });


        mBinding.play1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mRtcRoomDelegate.playFile("/sdcard/myvideo/1.mp4");
                mRtcRoomDelegate1.playFile("/sdcard/zzz/3.mp4");
            }
        });



        mBinding.start2.setOnClickListener(v -> getRoomRoken("room2"));

        mBinding.stop2.setOnClickListener(v -> {
            mRtcRoomDelegate2.stop();
        });


        mBinding.play2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRtcRoomDelegate2.playFile("/sdcard/zzz/4.mp4");
//                test();
            }
        });
    }


    private void getRoomRoken(String roomSessionId) {
        mClient.getRoomToken(roomSessionId, new RtcRoomerClient.ResultCallback<String>() {
            @Override
            public void onSuccess(String roomToken) {
                createRoom(roomSessionId, roomToken);
            }

            @Override
            public void onFail(int code, String msg) {

            }
        });
    }

    private boolean isReady1 = false;
    private boolean isReady2 = false;

    private void createRoom(String roomSessionId, String token) {

        if (roomSessionId.equals("room1")){
            Session session = new Session(roomSessionId, token, getApplicationContext());
            mRtcRoomDelegate1 = new RtcRoomDelegate(session);
            mRtcRoomDelegate1.run(() -> {
                isReady1 = true;
            });

        }else if (roomSessionId.equals("room2")){
            Session session = new Session(roomSessionId, token, getApplicationContext());
            mRtcRoomDelegate2 = new RtcRoomDelegate(session);
            mRtcRoomDelegate2.run(() -> {
                isReady2 = true;
            });
        }


    }

//    static {
//        System.loadLibrary("src-webrtc");
//    }

//    public native int add(int a, int b);

    private void test() {
//        Log.i("123123","a+b:"+add(1,4));
//        mRtcRoomDelegate.playFile("/sdcard/myvideo/2.mp4");
    }

}